package com.umeox.watch.moto.apiclient.http.adapter;

import android.text.TextUtils;

import com.umeox.watch.httpclient.core.ApiCallback;
import com.umeox.watch.httpclient.core.ApiRequest;
import com.umeox.watch.httpclient.core.ApiResponse;
import com.umeox.watch.httpclient.core.client.HttpApiClient;
import com.umeox.watch.httpclient.core.client.HttpClientBuilderParams;
import com.umeox.watch.httpclient.core.constant.HttpMethod;
import com.umeox.watch.httpclient.core.constant.Scheme;
import com.umeox.watch.moto.apiclient.http.callback.WaasCallback;
import com.umeox.watch.moto.apiclient.http.request.WaasRequest;
import com.umeox.watch.moto.apiclient.http.response.WaasResponse;
import com.umeox.watch.moto.apiclient.http.response.WaasResponseImpl;


public class FileApiClientAdapterImpl implements HttpClientAdapter {
    private HttpApiClientImpl apiClient = new HttpApiClientImpl();
    private String host;

    public FileApiClientAdapterImpl(Scheme scheme, String host) {
        HttpClientBuilderParams clientBuilderParams = new HttpClientBuilderParams();
        clientBuilderParams.setHost(TextUtils.isEmpty(host) ? "123" : host);
        clientBuilderParams.setScheme(scheme);
        if (scheme == Scheme.HTTPS) {
            HttpsHelp.SSLParams params = HttpsHelp.getTlsSocketFactory();
            clientBuilderParams.setSslSocketFactory(params.sSLSocketFactory);
            clientBuilderParams.setX509TrustManager(params.trustManager);
            clientBuilderParams.setHostnameVerifier(params.hostnameVerifier);
        }
        this.apiClient.init(clientBuilderParams);
        this.host = host;
    }

    private WaasResponseImpl convertData(ApiResponse response) {
        WaasResponseImpl waasResponse = new WaasResponseImpl();
        int code = response.getCode();
        waasResponse.setCode(code);
        if (200 == code) {
            waasResponse.setRawData(response.getBody());
        } else {
            waasResponse.setMessage("服务器繁忙，请稍后试试！");
        }
        return waasResponse;
    }

    @Override
    public WaasResponse send(WaasRequest request) {
        return convertData(apiClient.sendSyncRequest(buildRequest(request)));
    }

    @Override
    public void send(final WaasRequest waasRequest, final WaasCallback callback) {
        apiClient.sendAsyncRequest(buildRequest(waasRequest), new ApiCallback() {
            @Override
            public void onFailure(ApiRequest request, Exception e) {
                callback.onFailure(waasRequest, e);
            }

            @Override
            public void onResponse(ApiRequest apiRequest, ApiResponse apiResponse) {
                callback.onResponse(waasRequest, convertData(apiResponse));
            }
        });
    }

    private ApiRequest buildRequest(WaasRequest waasRequest) {
        HttpMethod method = waasRequest.getMethod();
        if (method == null) {
            method = HttpMethod.GET;
        }

        String host = waasRequest.getHost();
        if (TextUtils.isEmpty(host)) {
            host = this.host;
        }

        String path = waasRequest.getPath();
        ApiRequest request = new ApiRequest(method, path);
        request.setHost(host);

        if (HttpMethod.POST_FILE == method) {
            request.setFormParams(waasRequest.getParams());
        }
        return request;
    }

    static class HttpApiClientImpl extends HttpApiClient {
        @Override
        public ApiResponse sendSyncRequest(ApiRequest request) {
            return super.sendSyncRequest(request);
        }

        @Override
        public void sendAsyncRequest(ApiRequest request, ApiCallback callback) {
            super.sendAsyncRequest(request, callback);
        }
    }
}
